﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using IHISCETeim.Library;

namespace IHISCETeim.Web.Export.AgentSys
{
    public partial class AgentQltyPartsNeedStockList : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "/Agent/品情旧件管理.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "sheet1";
            }

        }

       
        protected override string SQLSort
        {
            get
            {
                return " QltyId ASC ";
            }
        }

        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string ItemCode
        {
            get
            {
                return GetRequestValue<string>("ItemCode", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string AgentName
        {
            get
            {
                return GetRequestValue<string>("AgentName", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string ClaimsNumber
        {
            get
            {
                return GetRequestValue<string>("ClaimsNumber", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }
        /// <summary>
        /// 显示记录
        /// </summary>
        public int ShowType
        {
            get
            {
                return GetRequestValue<int>("ShowType", 0);
            }
        }

        public string IssueDateForm
        {
            get
            {
                return GetRequestValue<string>("IssueDateForm", "");

            }
        }

        public string IssueDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(IssueDateForm))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("IssueDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(IssueDateForm).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

                // return GetRequestValue<string>("IssueDateTo", "");

            }
        }

        public string InDateForm
        {
            get
            {
                return GetRequestValue<string>("InDateForm", "");

            }
        }

        public string InDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(InDateForm))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("InDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(InDateForm).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

                // return GetRequestValue<string>("IssueDateTo", "");

            }
        }
        protected string GetSql()
        {
            string sqlwhere = QueryFiledHelper.GetSqlWhere(this);
            string OnlyWeiWCSql = "";

            if (ShowType != 0)
            {
                if (ShowType == 1)
                {
                    if (!string.IsNullOrEmpty(sqlwhere))
                        sqlwhere += " and (Quantity-InQuantity)>0 ";
                    else
                        sqlwhere = " (Quantity-InQuantity)>0 ";
                }
                if (ShowType == 2)
                {
                    if (!string.IsNullOrEmpty(sqlwhere))
                        sqlwhere += " and (Quantity-InQuantity)=0 ";
                    else
                        sqlwhere = " (Quantity-InQuantity)=0 ";
                }
                if (ShowType == 3)
                {
                    if (!string.IsNullOrEmpty(sqlwhere))
                        sqlwhere += " and inwilldate<=GETDATE()  AND InStock=1 AND InQuantity<>Quantity AND ISNULL(InWuPan,0)<=0 ";
                    else
                        sqlwhere = " inwilldate<=GETDATE()  AND InStock=1 AND InQuantity<>Quantity AND ISNULL(InWuPan,0)<=0";
                }
            }
            if (!string.IsNullOrEmpty(IssueDateForm))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("IssueDate", QueryOperatorsEnum.范围, IssueDateForm + "|" + IssueDateTo);
                if (!string.IsNullOrEmpty(sqlwhere))
                {
                    sqlwhere += " and " + DOWNSQL;
                }
                else
                {
                    sqlwhere = DOWNSQL;
                }
            }
            if (!string.IsNullOrEmpty(InDateForm))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("InDate", QueryOperatorsEnum.范围, InDateForm + "|" + InDateTo);
                if (!string.IsNullOrEmpty(sqlwhere))
                {
                    sqlwhere += " and " + DOWNSQL;
                }
                else
                {
                    sqlwhere = DOWNSQL;
                }
            }

            return sqlwhere;
        }

        private DataTableCollection _dts = null;
        protected DataTableCollection dts
        {
            get
            {
                if (_dts == null)
                {
                    int TotalCount = 0;
                    _dts = AgentQltyController.AgentQltyPartsGetPagination(GetSql(), 1, 999999999, SQLSort, ref TotalCount);
                }
                return _dts;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeWorkbook();
            ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
            //
            DataTable dtList = dts[0];

            int rowindexBase = 1;
            int rowindex = 0;
            int rowReal = rowindex + rowindexBase;


            foreach (DataRow listDTDR in dtList.Rows)
            {
                int cellReal = 0;
                HSSFRow writerow = (HSSFRow)sheet1.CreateRow(rowReal);
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ClaimsNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(GetDateStr(listDTDR["IssueDate"]));
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["SerialNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["AgentName"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ItemCode"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["Dscription"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["Quantity"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["PartsDemand"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(GetDateStr(listDTDR["SupperSendDate"]));
                writerow.CreateCell(cellReal++).SetCellValue(GetDateStr(listDTDR["RealSendDate"]));
                writerow.CreateCell(cellReal++).SetCellValue(GetDateStr(listDTDR["InWillDate"]));
                writerow.CreateCell(cellReal++).SetCellValue(GetDateStr(listDTDR["InDate"]));
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["InQuantity"].ToString());
                rowReal++;
            }

            //writerowSum.CreateCell(2).SetCellFormula(string.Format("sum(C3:C{0})", rowReal));          

            //
            WriteToFile();
        }

        private string GetDateStr(object v)
        {
            if (v is DBNull)
                return "";
            return Convert.ToDateTime(v).ToString("yyyy-MM-dd HH:mm");
        }

        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("{0}{1}.xls", DateTime.Now.ToString("yyyy-MM-dd"), "品情旧件管理").Trim();
        }
    }
}